"use strict";

class dbOperate {
    readyBuilder_Success = undefined;
    readyBuilder_Failure = undefined;

    constructor(DataBaseConfigure) {
        let curIns = this;
        this.___curDBinstance = undefined;
        this.__InstanceCollect = {
            mongo: require('./mongoCollect'),
            gtcpk: require('gtcpk-be')
        }
        try {
            this.__ToolKit = new this.__InstanceCollect.gtcpk();
        } catch (e) {
            this.__ToolKit = new this.__InstanceCollect.gtcpk.GeneralToolsCollectionPackageKit();
        }
        switch (Object.prototype.toString.call(DataBaseConfigure)) {
            case "[object Object]":
                if (this.__ToolKit.emptyObjectDetect(DataBaseConfigure) === false) {
                    for (let ConfigItem in DataBaseConfigure) {
                        this.___curDBinstance = String(ConfigItem).toLowerCase()
                        switch (String(ConfigItem).toLowerCase()) {
                            case "mongodb":
                                this.mongoPoint = new this.__InstanceCollect.mongo(DataBaseConfigure[ConfigItem]);
                                break;
                            case "mysqldb":
                                break;
                            case "sqlsvdb":
                                break;
                        }
                    }
                } else {
                    throw new Error("配置数据对象异常！");
                }
                break;
            case "[object Undefined]":
                this.mongoPoint = new this.__InstanceCollect.mongo();
                curIns.___curDBinstance = "unConfirm";
                break;
            default:
                throw new Error("配置数据必须是对象！");
        }
        let ccm = setInterval(() => {
            switch (curIns.___curDBinstance) {
                case "mongodb":
                    if (typeof curIns.mongoPoint.__InstanceRootControl.readyStatus === "boolean") {
                        if (curIns.mongoPoint.__InstanceRootControl.readyStatus) {
                            if (typeof curIns.readyBuilder_Success === "function") {
                                curIns.readyBuilder_Success();
                            }
                        } else {
                            if (typeof curIns.readyBuilder_Failure === "function") {
                                curIns.readyBuilder_Failure();
                            }
                        }
                        clearInterval(ccm);
                    }
                    break;
                case "mysqldb":
                    // if (typeof curIns.mongoPoint.__InstanceRootControl.readyStatus === "boolean") {
                    //     if (curIns.mongoPoint.__InstanceRootControl.readyStatus) {
                    //         if (typeof curIns.readyBuilder_Success === "function") {
                    //             curIns.readyBuilder_Success()
                    //         }
                    //     } else {
                    //         if (typeof curIns.readyBuilder_Failure === "function") {
                    //             curIns.readyBuilder_Failure()
                    //         }
                    //     }
                    // }
                    break;
                case "unConfirm":
                    clearInterval(ccm);
                    break;
            }
        }, 100);
    }
}

module.exports = dbOperate;